package Power_Asisst.main;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class Untils {
	 final TextView myTextView =(TextView)findViewById(R.id.textViewReceive);

	// lay dia chi hien tai tren may Server
	public String getLocalIPAddress()
	{
		try {
			for(Enumeration<NetworkInterface>en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
				NetworkInterface intf=en.nextElement();
				for(Enumeration<InetAddress>enumIpAddr=intf.getInetAddresses();enumIpAddr.hasMoreElements();){
					InetAddress inetAddress=enumIpAddr.nextElement();
					if(!inetAddress.isLoopbackAddress())
					{
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("Loi khi get LocalAddress",e.toString());
		}
		return null;
	}
	
	
	private TextView findViewById(int textviewreceive) {
		// TODO Auto-generated method stub
		return null;
	}


	// Chuyen doi chuoi thanh mang
	
	public String[] sliptIPToArr(String inputIP)
	{
			String[]result=inputIP.split("\\.");
			return result;
	}
	
	// Cat duoi cua ip 10.0.2.2 --> 10.0.2.x
	
	public String[] subIP(String[]inputIP)
	{
		String[] result = new String[inputIP.length];
		for(int i=0;i<inputIP.length-1;i++)
		{
			result[i]=inputIP[i];
		}
		return result;
	}
	
	// Cover to int 
	
	public String convert(int i)
	{
		return String.valueOf(i);
	}
	
	// Cover String[] to String
	
	public String arrayToString(String[]inputArr)
	{
		String result="";
		for(int i=0;i<inputArr.length;i++)
		{
			if(i!=0)
			{
				result=result+".";
			}
			result=result+inputArr[i];
		}
		return result;
	}
	
	
	// tao dai IP co do dai x.x.x.1 -> x.x.x.254
	
	public String[] myArryList(String[]subIP)
	{
		String[]result= new String[254];
		String[]newSubIP=subIP;
		String temp;
		for(int i=0;i<254;i++)
		{
			try {
				newSubIP[3]=convert(i);
    			temp=arrayToString(newSubIP);
    			result[i]=temp;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
		}
		return result;
	}
	
	// mod date 9/5/2012
	// Khue has change here. Slipt IP type string remove host nam in string 
	// PMKhue-pc, 192.168.1.1 to 192.168.1.1
	public String[] IPSlipt(String[]inputIP)
	{
		String[]result;
		result= new String[inputIP.length];
		String temp[];
		for(int i=0;i<inputIP.length;i++)
		{
			temp=inputIP[i].split(",");
			result[i]=temp[1];
		}
		return result;
	}
	
	// SetServer status in thread
	public void SetStatus(String abc)
	{
		myTextView.setText(abc);
	}

	
}
